import {roleSearch, SysRoleVo, SysUserVo, SysIdentityVo} from "./types";
import request from "@/service/request";
import {PromisePageResponse, PromiseResponse} from "@/service/api/type.ts";

/**
 * 获取角色列表
 */
const getRoleList = (query: roleSearch): PromisePageResponse<SysRoleVo> => {
  return request({
    url: "/system/role/list",
    method: "get",
    params: query,
  });
};

/**
 * 新建角色
 * @param query
 */
const addRole = (query: any): PromiseResponse<any> => {
  return request({
    url: "/system/role",
    method: "post",
    data: query,
  });
};

/**
 * 获取角色树
 */
const getDefaultRoleTree = () => {
  return request({
    url: "/system/permission/treeselect",
    method: "get",
  });
};

// 获取角色所有可视范围字段
// const getUserAllView = (id:string) => {
//     return request({
//         url:'/system/model/'+id,
//         method:'get'
//     })
// }

/**
 * 获取角色详细信息
 * @param roleId
 */
const getRoleDetail = (roleId: string): PromiseResponse<SysRoleVo> => {
  return request({
    url: `/system/role/${roleId}`,
    method: "get",
  });
};

/**
 * 修改角色信息
 * @param query
 */
const updateRole = (query: any): PromiseResponse<any> => {
  return request({
    url: "/system/role",
    method: "put",
    data: query,
  });
};

/**
 * 删除角色
 * @param id
 */
const deleteRole = (id: string | string[]): PromiseResponse<any> => {
  return request({
    url: "/system/role/" + id,
    method: "delete",
  });
};

/**
 * 获取成员列表
 * @param query
 */
const getRoleMemberList = (query?: any): PromisePageResponse<SysUserVo> => {
  return request({
    url: "/system/role/authUser/allocatedList",
    method: "get",
    params: query,
  });
};

/**
 * 角色添加成员
 * @param query
 */
const addUserToRole = (query: any): PromiseResponse<any> => {
  return request({
    url: "/system/user/authRole",
    method: "post",
    data: query,
  });
};

/**
 * 批量添加成员
 * @param roleId
 * @param userIds
 */
const addUsersToRole = (roleId: string, userIds: string | string[]): PromiseResponse<any> => {
  return request({
    url: `/system/user/authRole/selectAll/${roleId}`,
    method: "put",
    data: userIds,
  });
};
// const addUsers = (query: any) => {
//   return request({
//     url: "/system/role/authUser/selectAll",
//     method: "put",
//     data: query,
//   });
// }

/**
 * 获取身份列表
 */
const getIdentityArr = (): PromisePageResponse<SysIdentityVo> => {
  return request({
    url: "/system/identity/list",
    method: 'get',
  });
};

/**
 * 取消授权用户
 * @param query
 */
const removeUserFromRole = (query: any): PromiseResponse<any> => {
  return request({
    url: "/system/user/authRole/cancel",
    method: "put",
    data: query,
  });
};

/**
 * 批量取消授权用户
 * @param roleId
 * @param userIds
 */
const removeUsersFromRole = (roleId: any, userIds: any): PromiseResponse<any> => {
  return request({
    url: "/system/user/authRole/cancelAll/" + roleId,
    method: "delete",
    data: userIds,
  });
};

/**
 * 获取未分配角色的用户列表
 * @param query
 */
const getUserListNotInRole = (query: any): PromisePageResponse<SysUserVo> => {
  return request({
    url: "/system/role/authUser/unallocatedList",
    method: "get",
    params: query,
  });
};

export {
  getRoleList,
  addRole,
  getDefaultRoleTree,
  getRoleDetail,
  updateRole,
  deleteRole,
  getRoleMemberList,
  addUserToRole,
  removeUserFromRole,
  removeUsersFromRole,
  getUserListNotInRole,
  addUsersToRole,
  getIdentityArr
};
